<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Reg extends CI_Controller {
	
	function index(){
		if($this->input->post('act') == 'reg'){
			
			$ret['url_title'] = '返回注册页面';
			$ret['url_next']="javascript:history.go(-1)";
			
			$data = array(
				'username'      => trim($this->input->post('username')),
                'phone'      => trim($this->input->post('phone')),
//				'email'         => trim($this->input->post('email')),
				'password'		=> md5(md5($this->input->post('password'))),
				'invite_userid' => intval($this->input->get('invite')),
//				'email_status'  => '0',
				'type_id'       => '2',
				'add_time'      => time(),
				'addip'         => $this->input->ip_address(),
			);
			
			if ($data['username']==""){
				$ret['msg'] = '用户名不能为空';
			}elseif(!isset($data['username'][2])){
				$ret['msg'] = '用户名太短';
			}elseif(!$this->myformcheck->isName($data['username'])){
				$ret['msg'] = '非法的用户名';
			}elseif($data['phone']==""){
				$ret['msg'] = '手机不能为空';
			}
//            elseif(!$this->myformcheck->isEmail($data['email'])){
//				$ret['msg'] = '无效的邮箱';
//			}
            elseif ($this->input->post('password')==""){
				$ret['msg'] = '密码不能为空';
			}elseif($this->input->post('password') != $this->input->post('password2')){
				$ret['msg'] = '两次密码不一致';
			}else{
				$this->load->model('User_model', 'user');
				if($this->user->get_one(array('username'=>$data['username']), 'user_id')){
					$ret['msg'] = '用户名已存在';
				}elseif($this->user->get_one(array('phone'=>$data['phone']), 'user_id')){
					$ret['msg'] = '邮箱已存在';
				}else{
					/** UC同步注册 */
//					include './config.inc.php';
//					include './uc_client/client.php';
//					$uid = uc_user_register(iconv('UTF-8', 'GBK', $data['username']), $this->input->post('password'), $data['email']);
//					if($uid <= 0) {
//						if($uid == -1) {
//							$ret['msg'] = '非法的用户名';
//						} elseif($uid == -2) {
//							$ret['msg'] = '非法的用户名';
//						} elseif($uid == -3) {
//							$ret['msg'] = '用户名已存在';
//						} elseif($uid == -4) {
//							$ret['msg'] = '无效的邮箱';
//						} elseif($uid == -5) {
//							$ret['msg'] = '该邮箱不允许注册';
//						} elseif($uid == -6) {
//							$ret['msg'] = '邮箱已存在';
//						} else {
//							$ret['msg'] = '论坛注册失败，请与客服联系';
//						}
//					}else{
						$user_id = $this->user->add($data);
						if($user_id){
//							$msg = array(
//								'user_id'=> $user_id,
//								'username' => $data['username'],
//								'webname' => CON_PLATFORMNAME,
//								'email' => $data['email']
//							);
							//发送注册邮件
//							$this->load->library('func');
//							$this->load->library('/phpmailer/PHPMailer');
//							$msgBody = $this->func->regEmailMsg($msg);
//							$this->phpmailer->IsSMTP();
//							$this->phpmailer->AddAddress($data['email'], $data['username']);//收件人
//							$this->phpmailer->Subject 	= CON_PLATFORMNAME."注册认证";
//							$this->phpmailer->Body 		= $msgBody;
//							$res = $this->phpmailer->Send();

                            //添加体验金
                            $this->load->model('Account_model');
                            $initAccount = array(
                                'user_id' => $user_id,
                                'total' => '0',
                                'use_money' => '0',
                                'no_use_money' => '0',
                                'collection' => '0',
                                'reg_money'=>1000
                            );
                            $this->Account_model->init_account($initAccount);

							//注册成功自动登陆
							$user = array(
								'user_id' => $user_id,
								'username' => $data['username'],
								'email' => $data['email'],
								'login_time' => time()
							);
							$this->session->set_userdata('ych_user',$user);
							
							//echo "<script>location.href='/user/center'</script>";
							$ret['msg'] = '注册成功，请尽快前往邮箱激活';
							$ret['url_title'] = '前往用户中心';
							$ret['url_next']="/mobile/ucenter.html";
						}else{
							$ret['msg'] = '注册失败，请联系客服';
						}
//					}
				}
			}
			$this->load->view('mobile/msg',$ret);
			
		}else{
			$this->load->view('mobile/reg');
		}
	}

    public function apply_phone($id = ''){
        $this->load->model('User_model', 'user');
        $ret = array(
            'msg' => '',
            'url' => '',
        );

        $k = md5('ychdx');
        if(isset($_COOKIE[$k]) && $_COOKIE[$k] == 1){
            $ret['msg'] = '60秒内只能获取一次短信验证码，请稍后再试';
            echo json_encode($ret);
            exit;
        }else{
            $phone = $this->input->post('phone');
            if(!$phone){
                $ret['msg'] = '请填写手机号码';
            }elseif(!$this->myformcheck->isPhone($phone)){
                $ret['msg'] = '请填写有效的手机号码';
            }else{

                if($id == ''){//如果不是找回登录密码的要验证手机重复
                    // 验证手机号码是否存在
                    $tmpwhere = array(
                        'phone'=>$phone,
                    );
                    $row = $this->user->get_one($tmpwhere,'user_id');
                    if(isset($row['user_id'])){
                        $ret['msg'] = '该手机号码已存在';
                    }
                }
            }
            if($ret['msg']){
                echo json_encode($ret);
                exit;
            }

            $md5 = rand_string($phone,6,1,13);

            if($id == 12){
                $res = $this->Notice(12, 0, array('vcode'=>$md5,'phone'=>$phone));
                $this->session->set_userdata('ych_temp_captcha',$md5);
            } else {
                $res = $this->Notice(4, 0, array('vcode'=>$md5,'phone'=>$phone));
                $this->session->set_userdata('ych_temp_userid',$md5);
            }


            if($res){
                $ctime = time()+60;//60秒内只能发送一次短信息
                setcookie($k,1,$ctime);

                $ret['msg'] = '手机验证码已发送，请注意查收';


                $ret['info'] = '1';
                echo json_encode($ret);
                exit;
            }else{
                $ret['msg'] = '短信发送失败，请稍后再试或联系客服';
                echo json_encode($ret);
                exit;
            }

            echo json_encode($ret);
            exit;

        }

    }
	
}